package util;

import java.sql.*;

import javax.swing.JOptionPane;



public class DBUtil {
		//定义需要的对象
		static java.sql.PreparedStatement ps=null;
		static ResultSet rs=null;
		static Connection ct=null;
		static String driver="com.mysql.jdbc.Driver";
		//URL指向要访问的数据库名
		static String url="jdbc:mysql://127.0.0.1:3306/youyannongdu";
		//mysql配置时的用户名
		static String user="root";
		//mysql配置时的密码
		static String password="wl110748";
		//构造函数，初始化ct
		public static Connection open()
		{
			try {
				//加载驱动
				Class.forName(driver);
				ct=DriverManager.getConnection(url,user,password);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				JOptionPane.showMessageDialog(null, "数据库加载失败！","错误提示",JOptionPane.ERROR_MESSAGE);
			}finally{
				
			}
			return ct;
		}
		
	      
	      //关闭资源的方法
	      public static void close()
	      {
	    	  try {
				if(rs!=null) rs.close();
				  if(ps!=null) ps.close();
				  if(!ct.isClosed())  ct.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	      }
	      
	}


